<template>
    <div class="pagination">
        <button :disabled="pageNo === 1" @click="nowPage -= 1">上一页</button>
        <button
            v-show="startEnd.start > 1"
            @click="nowPage = 1"
            :class="nowPage === 1 ? 'active' : ''"
        >
            1
        </button>

        <button v-show="startEnd.start > 2">···</button>

        <!-- 
      避免v-if和v-for的一起使用
     -->
        <button
            v-for="item in pageContainer"
            :key="item"
            @click="nowPage = item"
            :class="nowPage === item ? 'active' : ''"
        >
            {{ item }}
        </button>

        <button v-show="startEnd.end < totalPages - 1">···</button>
        <button
            v-show="startEnd.end < totalPages"
            @click="nowPage = totalPages"
            :class="nowPage === totalPages ? 'active' : ''"
        >
            {{ totalPages }}
        </button>
        <button :disabled="pageNo === totalPages" @click="nowPage += 1">
            下一页
        </button>

        <button style="margin-left: 30px">共 60 条</button>
    </div>
</template>

<script>
export default {
    name: "Pagination",
    props: ["totalPages", "pageNo", "continueNum"],
    data() {
        return {
            nowPage: this.pageNo,
        };
    },
    computed: {
        startEnd() {
            let start = 1;
            let end = 1;
            //首先判断总页数是否小于连续页长度,连续页的开始就是1 结束就是连续页的长度
            if (this.totalPages < this.continueNum) {
                start = 1;
                end = this.totalPages;
            } else {
                //连续页长度小于总长度
                // 根据当前页和连续页长度 计算出连续页开头 和 连续页结尾 的值
                start = this.pageNo - Math.floor(this.continueNum / 2);
                end = this.pageNo + Math.floor(this.continueNum / 2);

                //还要判断,因为start可能小于1,让start等于1 end等于连续页
                if (start < 1) {
                    start = 1;
                    end = this.continueNum;
                }

                //还要判断,因为end的值可能大于totalPages,让end变成总页数,计算start
                if (end > this.totalPages) {
                    end = this.totalPages;
                    start = this.totalPages - this.continueNum + 1;
                }
            }

            return {
                start,
                end,
            };
        },
        pageContainer() {
            const { start, end } = this.startEnd;
            const arr = [];
            for (var i = start; i <= end; i++) {
                arr.push(i);
            }
            return arr;
        },
    },
    watch: {
        nowPage() {
            this.$emit("changePageNo", this.nowPage);
        },
    },
};
</script>

<style lang="less" scoped>
.pagination {
    button {
        margin: 0 5px;
        background-color: #f4f4f5;
        color: #606266;
        outline: none;
        border-radius: 2px;
        padding: 0 4px;
        vertical-align: top;
        display: inline-block;
        font-size: 13px;
        min-width: 35.5px;
        height: 28px;
        line-height: 28px;
        cursor: pointer;
        box-sizing: border-box;
        text-align: center;
        border: 0;

        &[disabled] {
            color: #c0c4cc;
            cursor: not-allowed;
        }

        &.active {
            cursor: not-allowed;
            background-color: #409eff;
            color: #fff;
        }
    }
}
</style>
